package com.atguigu.flink.chapter06;

import com.atguigu.flink.bean.AdsClickLog;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * TODO
 *
 * @author cjp
 * @version 1.0
 * @date 2021/8/10 16:24
 */
public class Flink08_AdClick {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 1.读取数据
        SingleOutputStreamOperator<AdsClickLog> adClickDS = env
                .readTextFile("F:\\atguigu\\01_course\\code\\flink210323\\input\\AdClickLog.csv")
                .map(r -> {
                    String[] datas = r.split(",");
                    return new AdsClickLog(
                            Long.parseLong(datas[0]),
                            Long.parseLong(datas[1]),
                            datas[2],
                            datas[3],
                            Long.parseLong(datas[4])
                    );
                });

        // 2.处理数据
        // 2.1 按照 维度 分组： 省份、广告
        adClickDS
//                .keyBy(r -> Tuple2.of(r.getProvince(), r.getAdId())) // 之后用 process 去计数
                .map(r -> Tuple2.of(r.getProvince() + "_" + r.getAdId(), 1))
                .returns(Types.TUPLE(Types.STRING,Types.INT))
                .keyBy(r -> r.f0)
                .sum(1)
                .print();

        env.execute();
    }
}
